/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author neo
 */
public class ListToStringHandler extends ObjectToStringHandler {

    @Override
    public String toString(Object obj1, Properties prop) {
        StringBuffer s = new StringBuffer();
        s.append("reObj = new List();");
        List list = (List) obj1;
        for (int x = 0; x < list.size(); x++) {
            Object o = list.get(x);
            s.append("reObj.add({");
            s.append("javaClass:\"").append(o.getClass().getName()).append("\",");
            try {
                Method[] methods = o.getClass().getDeclaredMethods();
                for (int i = 0; i < methods.length; i++) {
                    Method method = methods[i];
                    if (method.getName().startsWith("get")) {
                        System.out.println("Obj["+ o.getClass().getName() +"] Method :"+ method.getName());
                        String mName = method.getName().substring(3);
                        mName = mName.substring(0, 1).toLowerCase() + mName.substring(1);
                        s.append("\"").append(mName).append("\": ");

                        Object reObj = method.invoke(o, new Object[]{});
                        if( reObj == null ) {
                            continue;
                        }

                        String sCase = "";
                        //System.out.println("scase["+ sCase +"]");
                        try {
                            sCase = reObj.getClass().getName();
                        } catch(Exception ex) {
                            System.out.println("err Obj["+ o.getClass().getName() +"] Method :"+ method.getName());
                            ex.printStackTrace();
                        }
                        int iCase = Integer.parseInt(prop.getProperty(sCase + ".return.ptype"));
                        switch (iCase) {
                            case 0:
                                break;
                            case 1:
                                s.append("\"").append((String) reObj).append("\"");
                                break;
                            case 2: {
                                int val = ((Integer) reObj).intValue();
                                s.append(val);
                            }
                            break;
                            case 3: { //long
                                long val = ((Long) reObj).longValue();
                                s.append(val);
                            }
                            break;
                            case 4: { //double
                                double val = ((Double) reObj).doubleValue();
                                s.append(val);
                            }
                            break;
                            case 5: { //float
                                float val = ((Float) reObj).floatValue();
                                s.append(val);
                            }
                            break;
                            case 6: { //Date
                                java.util.Date ds = (java.util.Date) reObj;
                                SimpleDateFormat sf = new SimpleDateFormat(prop.getProperty("date.format"));
                                String date = sf.format(ds);
                                s.append("\"").append(date).append("\"");
                            }
                            break;
                            case 7: { //boolean
                                boolean val = ((Boolean) reObj).booleanValue();
                                s.append(val);
                                s.append(reObj);
                            }
                            break;
                            default:
                                s.append("\"").append(reObj.toString()).append("\"");
                                break;
                        }
                        s.append(",");
                    }
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            s.delete(s.length() - 1, s.length());
            s.append("});");
        }
        return s.toString();
    }
}
